<?php

class TableViewView {

    private static $me = NULL;

    public static function getInstance() {
        if (!self::$me instanceof TableViewView) {
            self::$me = new TableViewView();
        }

        return self::$me;
    }

    public function getTableViewTemplate($tableID){
        $xtpl = new XTemplate(Utility::getFileContent(DIR_TEMPLATES . DS . 'table.view.main.tpl.html'));
        $xtpl->assign('table', new Table($tableID));
        $fieldIDs = TableManager::getInstance()->getTableFields($tableID);
        $fields = '';
        foreach ($fieldIDs as $fieldID) {
            $fields .= $this->getPopulatedFieldRowTemplate($fieldID);
        }
        $xtpl->assign('field_rows', $fields);
        $xtpl->parse('main');
        return $xtpl->out('main');
    }
    private function getPopulatedFieldRowTemplate($fieldID){
        $xtpl = new XTemplate(Utility::getFileContent(DIR_TEMPLATES . DS . 'table.view.field.tpl.html'));
        $field = new Field($fieldID);
        
        $xtpl->assign('field', $field);
        $checked = '';
        if($field->getIsPrimary()){
            //$xtpl->assign('primary_checked', 'checked="checked"');
        }
        else{
            //$xtpl->assign('primary_checked', '');
        }
        $xtpl->assign('rowColor', Utility::getRowColor());
        $xtpl->parse('main');
        return $xtpl->out('main');
    }

}

?>
